/*
ID: khymill1
PROG: prefix 
LANG: C++
*/
#include <string.h>
#include <vector>
#include <list>
#include <map>
#include <set>
#include <queue>
#include <deque>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <fstream>
#include <set>
#include <limits.h>

using namespace std;

int main()
{
    ifstream fin("prefix.in");
    ofstream fout("prefix.out");
    string str;
    set<string> prim;
    int maxLen = 0;
	int len = 0;
    while(true) {
        string in;
        fin >> in;
        if (in == ".") break;
		len = max(len, (int)in.size());
        prim.insert(in);
    }
    while (!fin.eof())
    {
      string input;
	  fin >> input;
      str += input;
    }
    vector<int> dp(str.size()+1, 0);
	vector<string> debug(str.size()+1);
    dp[0] = 0;
    bool hasPrefix = false;

    for (int i = 1; i <= str.size(); i++) {
        int count = 0;
        for (int j = max(1, i - len-1); j <=i; j++) {
            if (prim.count(str.substr(j-1, i-j+1)) == 0) {
                count = max(count, dp[j-1]);
            } else {
                if (j == 1)
					hasPrefix = true;
                if (count <=  dp[j-1] + i-j+1) {
					if (j == 10)
						int test = 0;
					debug[i] = str.substr(j-1,i-j+1);
					count = dp[j-1]+i-j+1;
				}
            }
			dp[i] = count;
        }
    }

	int i ;
	for (i = str.size(); i > 0; i--)
		if (dp[i] == i) {
			break;

		}

	if (hasPrefix) fout << i << endl;
    else fout << "0" << endl;
    return 0;
}                                          
